package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.nimbusds.jose.JOSEException;
import com.woniuxy.entity.DTO.*;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import com.woniuxy.util.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private MinioUtil minioUtils;

    @PostMapping("/queryUser")
    public ResponseData queryAllUser(@RequestBody PageParam<User> pageParam){
        if (pageParam.getParam().getDate()!=null) {
            Date startDate = pageParam.getParam().getDate()[0];
            Date endDate = pageParam.getParam().getDate()[1];
            pageParam.getParam().setStartTime(startDate);
            pageParam.getParam().setEndTime(endDate);
        }
        PageInfo pageInfo = userService.queryUser(pageParam);
        return ResponseData.success(pageInfo);
    }
    @PostMapping("/register")
    public ResponseData register(@RequestBody UserRegisterDTO userRegisterDTO){
       return   userService.register(userRegisterDTO);

    }
    @PostMapping("/login")
    public ResponseData login(@RequestBody UserLoginDTO userLoginDTO){

        try {
            return userService.login(userLoginDTO);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @GetMapping("/queryUserInfo")
    public ResponseData queryUserInfo() {

        try {
          return userService.queryUserInfo();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/imgLoad")
    public ResponseData imgLoad(MultipartFile file, HttpServletRequest request){
        try {
            String imgPath = minioUtils.uploadFile(file, "image");
            Integer userId = UserContext.getUserId();
            userService.imgLoad(userId, imgPath);
            return ResponseData.success(imgPath);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/Backlogin")
    public ResponseData Backlogin(@RequestBody BackLoginDTO backLoginDTO){
     return userService.Backlogin(backLoginDTO);
    }
    @PostMapping("/updateAdministrator")
    public ResponseData updateAdministrator(@RequestBody BackLoginDTO backLoginDTO){
        return userService.updateAdministrator(backLoginDTO);
    }
    @GetMapping("/queryAdministratorInfo/{administratorId}")
    public ResponseData queryAdministratorInfo(@PathVariable Integer administratorId){

        return userService.queryAdministratorInfo(administratorId);
    }
    @PostMapping("/imgSrcLoad")
    public ResponseData imgSrcLoad(@RequestBody MultipartFile file, HttpServletRequest request){
        try {
            String imgPath = minioUtils.uploadFile(file, "image");
            userService.imgSrcLoad(1, imgPath);
            return ResponseData.success(imgPath);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/updateAdministratorPassword")
    public ResponseData updateAdministratorPassword(@RequestBody UpdateAdministratorPasswordDTO updateAdministratorPasswordDTO){
        userService.updateAdministratorPassword(updateAdministratorPasswordDTO);
        return ResponseData.success();
    }
    @PostMapping("/updateLoginTime")
    public ResponseData updateLoginTime(){
        try {
            userService.updateLoginTime();
            return ResponseData.success();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }
}
